package com.topway.dsmflex.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

import org.apache.log4j.Logger;

import com.Consts;

public class ObjectSerializable {
	
	private static Logger log = Logger.getLogger(ObjectSerializable.class);
	
	
    public static void writeObject(String pathFile,Object obj)
    {
    	try {
    		File tempFile = new File(pathFile);
    		if(tempFile.isFile())
    			tempFile.delete();
    		tempFile.createNewFile();
    	 	FileOutputStream f = new FileOutputStream(tempFile);
    	    ObjectOutputStream objectOut = new ObjectOutputStream(f);
    	    objectOut.writeObject(obj);
    	    objectOut.writeObject(null);
    	    objectOut.close();
		} catch (Exception e) {
			log.error(e.getMessage()+":"+pathFile);
		}
    }
    
    public static Object readObject(String pathFile)
    {
    	Object objEditor = null;
     	try {
            InputStream inputSteam = new FileInputStream(pathFile);
            ObjectInputStream oisEditor = new ObjectInputStream(inputSteam);
            objEditor = oisEditor.readObject();
		} catch (Exception e) {
			log.error(e.getMessage()+":"+pathFile);
		}
        return (Object) objEditor;
    }
    
    
    
    
    public static void main(String[] args) {
//    	Map<String,String> map = new HashMap<String,String>();
//		map.put("liushengye", "��ʢҵ");
//		ObjectSerializable.writeObject(Consts.DATAFILE, map);
    	Map<String,String> map = (Map<String, String>) ObjectSerializable.readObject(Consts.DATAFILE_TABLE);
    	System.out.println(map.get("liushengye"));
    }
}
